
const path=require('path')
const isDev=process.env.NODE_ENV=="development"
const HTMLPlugin=require('html-webpack-plugin')
const webpack=require('webpack')
const ExtractPlugin=require('extract-text-webpack-plugin')

const config={
    target:'web',
    entry:path.join(__dirname,'src/index.js'),
    output:{
        filename:'bundle.[hash:8].js',
        path:path.join(__dirname,'dist')
    },
    module:{
        rules:[
            {
                test : /\.vue$/,
                loader : 'vue-loader'
            },{
                test:/\.jsx$/,
                loader:'babel-loader'
            },/*{
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader'
                ]
            },*/{
                test: /\.(gif|jpg|jpeg|png|svg)$/,
                use: [
                    {
                        loader:'url-loader',
                        options: {
                            limit: 1024*20,   
                            name: '[name]-gen.[ext]'
                        }
                    
                    }
                ]
            }

        ]
    },
    plugins:[
        new webpack.DefinePlugin({
            'process.env':{
                NODE_ENV: isDev?'"development"':'"production"'
            }
        }),
        new HTMLPlugin()
    ]

}

//判断环境
if(isDev){
    config.module.rules.push({
        test: /\.styl$/,
        use: [
            'style-loader',
            'css-loader',
            {
                loader:'postcss-loader',
                options:{
                    sourceMap:true,
                }
            },
            'stylus-loader'
        ]
    })
    config.devtool='#chep-module-eval-source-map'
    config.devServer = {
        port: 8000,
        host: '127.0.0.1',
        overlay: {
            errors:true,
        }
        //,historyFallback:{}
        ,open:true   //自动打开
        ,hot:true   //热加载
    }
    config.plugins.push(
        new webpack.HotModuleReplacementPlugin(),  //热加载
        new webpack.NoEmitOnErrorsPlugin()
    )
}else{
    config.output.filename="[name].[chunkhash:8].js"
    config.module.rules.push({
        test: /\.styl$/,
        use: ExtractPlugin.extract({
            fallback:'style-loader',
            use: [
                'css-loader',
                {
                    loader:'postcss-loader',
                    options:{
                        sourceMap:true,
                    }
                },
                'stylus-loader'
            ]
        })
    })
    config.plugins.push(
        new ExtractPlugin('style.[contentHash:8].css'),
        new webpack.optimize.CommonsChunkPlugin({
            name:'vendor'
        }),
        new webpack.optimize.CommonsChunkPlugin({
            name:'runtime'
        })

    )
    /*  //webpack4 的写法 
    config.optimization={
        splitChunks:{
            cacheGroups: {
                commons: {
                    test: /[\\/]node_modules[\\/]/,
                    name: "vendor",
                    chunks: "all"
                }
            }
        }
    }*/
    config.entry={
        app:path.join(__dirname,'src/index.js'),
        vendor:['vue','storejs']
    }
}


module.exports=config